<template>
  <div class="merchat-list">
    <Breadcrumb>
      <BreadcrumbItem v-for="(name, i) in breadcrumb" :key="i">{{ name }}</BreadcrumbItem>
    </Breadcrumb>
    <div class="list-title">查询条件</div>
    <Row type="flex">
      <Form ref="formInline" :model="formInline" inline :label-width="0" label-position="left">
        <FormItem label="" prop="companyName">
          <Input type="text" v-model="formInline.companyName" placeholder="公司名称" clearable style="width: 150px;"/>
        </FormItem>
        <FormItem label="" prop="name">
          <Input type="text" v-model="formInline.name" placeholder="楼盘名称" clearable style="width: 150px;"/>
        </FormItem>
        <div class="form-but">
          <Button type="success" @click="getList">查询</Button>
          &nbsp;&nbsp;
          <Button @click="handleReset('formInline')">清空</Button>
        </div>
      </Form>
    </Row>
    <div>
      <Table class="list-table"
        stripe
        border
        ref="selection"
        :columns="columns"
        :data="dataList"
      ></Table>
      <div class="table-page">
        <Page :total="total" show-sizer show-elevator show-total @on-change="changePageNum" @on-page-size-change="changePageSize" />
      </div>
      <Spin size="large" fix v-if="spinShow">正在补发,不要着急,不要着急...</Spin>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import { formatTime } from '@/utils'
import Api from '@/api'

const premisesApi = Api.Premises
const reportApi = Api.report
const commonApi = Api.Common

export default {
  name: 'login',
  components: {
  },
  data() {
    return {
      breadcrumb: ['系统管理', '楼盘列表'],
      formInline: {
        companyName: '',
        name: '',
      },
      dataList: [],
      columns: [
        {
          title: '主键Id',
          key: 'id',
          minWidth: 80
        },
        {
          title: '公司名称',
          key: 'companyName',
          minWidth: 200
        },
        {
          title: '楼盘名称',
          key: 'name',
          minWidth: 200
        },
        {
          title: '创建时间',
          key: 'createTime',
          minWidth: 180,
          render: (h, params) => (
            h('div', {}, formatTime(params.row.createTime, 'second'))
          )
        },
        {
          title: '审核状态',
          key: 'premisesStatus',
          minWidth: 100
        },
        {
          title: '销售状态',
          key: 'saleStatus',
          minWidth: 100
        },
        {
          title: '报备方式',
          key: 'reportWay',
          minWidth: 140
        },
        {
          title: '重复开关',
          key: 'repeatReportFlag',
          minWidth: 100
        },
        {
          title: '报备保护时间',
          key: 'reportProtect',
          minWidth: 120
        },
        {
          title: '报备保护时间类型',
          key: 'reportProtectType',
          minWidth: 140
        },
        {
          title: '最短报备时间',
          key: 'specialTime',
          minWidth: 120
        },
        {
          title: '最短报备时间类型',
          key: 'specialTimeType',
          minWidth: 140
        },
        {
          title: '到访延长时间',
          key: 'visitProtect',
          minWidth: 120
        },
        {
          title: '到访延长时间类型',
          key: 'visitProtectType',
          minWidth: 140
        },
        {
          title: '交互保护时间',
          key: 'saveDays',
          minWidth: 120
        },
        {
          title: '交易保护时间类型',
          key: 'saveDaysType',
          minWidth: 140
        },
        {
          title: '到访延长开关',
          key: 'visitProtectSwitch',
          minWidth: 120
        },
        {
          title: '复访开关',
          key: 'repeatDelayDealTime',
          minWidth: 100
        },
        {
          title: '项目经理',
          key: 'manager',
          minWidth: 100
        },
        {
          title: '经理电话',
          key: 'mobile',
          minWidth: 120
        },
        {
          title: '项目总监',
          key: 'chier',
          minWidth: 100
        },
        {
          title: '总监电话',
          key: 'chiefMobile',
          minWidth: 120
        },
        {
          title: '操作',
          key: 'operate',
          minWidth: 430,
          fixed: 'right',
          render: (h, params) => (
            h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '15px'
                },
                on: {
                  click: () => {
                    this.$Message.success('详情页暂未实现！');
                  }
                }
              }, '查看'),
              h('Button', {
                props: {
                  type: 'error',
                  size: 'small'
                },
                style: {
                  marginRight: '15px'
                },
                on: {
                  click: () => {
                    this.$Modal.confirm({
                      title: '删除楼盘',
                      content: '确定删除此楼盘？',
                      onOk: () => {
                        this.deletePremises(params.row.id);
                      }
                    })
                  }
                }
              }, '删除'),
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '15px'
                },
                on: {
                  click: () => {
                    this.$Modal.confirm({
                      title: '推送楼盘',
                      content: '确定推送此楼盘给商户绑定的所有分销商？',
                      onOk: () => {
                        this.pushPremisesToV10(params.row.companyId, null, new Array(1).fill(params.row.id));
                      }
                    })
                  }
                }
              }, '推送V10'),
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '15px'
                },
                on: {
                  click: () => {
                    this.$Modal.confirm({
                      title: '清空楼盘失效报备',
                      content: '确定清空此楼盘所有的失效报备？',
                      onOk: () => {
                        this.clearInvalidReport(params.row.id);
                      }
                    })
                  }
                }
              }, '清空失效报备'),
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '15px'
                },
                on: {
                  click: () => {
                    this.$Modal.confirm({
                      title: '刷新楼盘置业顾问报备关联关系',
                      content: '确定刷新此楼盘所有置业顾问的报备关联关系？',
                      onOk: () => {
                        this.refreshAdviser(params.row.id);
                      }
                    })
                  }
                }
              }, '刷新置业顾问')
            ])
          )
        }
      ],
      page: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      spinShow: false
    }
  },
  methods: {
    // 分页变化
    changePageNum(pageNum) {
      this.getList({ pageNum })
    },
    // 条数变化
    changePageSize(pageSize) {
      this.getList({ pageSize })
    },
    async handleReset(name) {
      this.formInline.companyName = ''
      this.formInline.name = ''
      this.getList()
    },
    async getList(paramsPage = null) {
      let pageNum
      let pageSize
      let page
      const statePage = this.page
      if (paramsPage) {
        pageNum = paramsPage.pageNum || statePage.pageNum
        pageSize = paramsPage.pageSize || statePage.pageSize
        page = { pageNum, pageSize }
        this.page = { pageNum, pageSize }
      } else {
        page = statePage
      }
      const formInline = this.formInline
      const value = {}
      Object.keys(formInline).forEach(key => {
        if (formInline[key]) {
          value[key] = formInline[key]
        }
      })
      const params = {
        page,
        ...value
      }
      const { responseCode, data } = await this.$request(premisesApi.listPremises, params)
      this.dataList = [];
      this.totalCount = 0;
      if (responseCode === '1') {
        if (data.list) {
          this.dataList = data.list
          this.total = data.totalCount
        }
      }
    },
    async pushPremisesToV10(devCompanyId, agentCompnayId, premisesIds) {
      const { responseCode, responseMessage, responseType } = await this.$request(reportApi.pushPremisesToV10, { devCompanyId, agentCompnayId, premisesIds })
      if (responseCode === '1') {
        this.$Message.success('推送成功！')
        this.page = { pageNum: 1, pageSize: 10 }
        this.getList()
      } else if (responseType === 'ERROR') {
        this.$Message.error(responseMessage)
      } else {
        this.$Message.error('推送失败！')
      }
    },
    async clearInvalidReport(premisesId) {
      const { responseCode, data } = await this.$request(commonApi.clearInvalidReport, { premisesId })
      if (responseCode === '1') {
        this.$Message.success('清空成功！')
      } else {
        this.$Message.error('清空失败失败！')
      }
    },
    async deletePremises(id) {
      const { responseCode, data } = await this.$request(premisesApi.deletePremises, { id })
      if (responseCode === '1') {
        this.$Message.success('删除成功！');
        this.getList()
      } else {
        this.$Message.error('删除失败！')
      }
    },
    async refreshAdviser(premisesId) {
      const { responseCode, responseMessage } = await this.$request(commonApi.refreshAdviser, {premisesId})
      if (responseCode === '1') {
        this.$Message.success('刷新成功！');
        this.getList()
      } else {
        this.$Message.error(responseMessage);
      }
    },
  },
  mounted() {
    this.getList()
  },
}
</script>
<style lang="scss" scoped>
.merchat-list {
  .ivu-form-item {
    margin-right: 20px;
  }
  .form-but {
    display: inline-block;
    margin-bottom: 24px;
  }
  .list-title {
    height: 50px;
    line-height: 50px;
    font-size: 18px;
    color: #333;
  }
  .add {
    margin-bottom: 10px;
  }
  .table-page {
    display: flex;
    justify-content: space-between;
    margin-top: 15px;
  }
  .ivu-table .table-success-row td{
    background-color: greenyellow;
    color: #fff;
  }
  .ivu-table .table-fail-row td{
    background-color: red;
    color: #fff;
  }
}
</style>
